package com.example.shushiyunsst.util

import com.example.library_common.preference.Preference
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.fragment.getUserInfo
import com.example.shushiyunsst.fragment.main.constant.CURRENT_SIGNED_UP_KEY
import com.example.wallet.global.AppGlobals
import okhttp3.Interceptor
import okhttp3.Response
import kotlin.math.abs

/**
 *
 * */
class RequestInterceptor : Interceptor {

    var lastRequestTime = 0

    override fun intercept(chain: Interceptor.Chain): Response {
        val credentials = Preference.HOLDER.getCustomAppProfile(CURRENT_SIGNED_UP_KEY) ?: ""
        val basic = AppGlobals.getApplication()
            .getString(R.string.baseBearerAuth, credentials)
        val original = chain.request()
        val request = original.newBuilder()
            .header("Authorization", basic)
//            .header("Content-Type", "application/json")
//            .method(original.method, original.body)
//            .addHeader("Authorization", "Bearer\t$signedUpToken")
            .build()
//        val currentTimeMillis = System.currentTimeMillis()
//        if (abs(lastRequestTime - currentTimeMillis) > 2000) {
//            getUserInfo(AppGlobals.getApplication())
//            getUserBalanceInfo(AppGlobals.getApplication())
//            lastRequestTime = currentTimeMillis.toInt()
//        }

        return chain.proceed(request)
    }
}